unit Spielsystem;

interface

Uses Team;

Type SystemTyp = ( Heimmannschaft, Auswaertsmannschaft );

Type TPaarung = Class(TObject)
     Spieltag,
     Heim,
     Auswaerts,
     SystemHeim,
     SystemAuswaerts   : Integer;
     Hinrunde,
     TerminBerechnet   : Boolean;
     Termin            : TDateTime;
     Zeit              : TZeit;
     OhneTerminVerlegt : Boolean;

     Constructor Create(Sp,H,A:Integer;Hinr:Boolean);
End;

Const MaxMannschaften = 16;
      MaxPaarungen    = MaxMannschaften Div 2;

Type MannschaftsGrundtyp = 1..MaxMannschaften;
     Mannschaftsmenge = Set Of MannschaftsGrundtyp;

Const  SystemC       : Array [2..MaxPaarungen,1..MaxMannschaften-1,1..MaxPaarungen,SystemTyp] of Byte =
                       ((((1,4),(2,3),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((4,3),(1,2),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((2,4),(3,1),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0))),

                        (((1,6),(2,5),(3,4),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((6,4),(5,3),(1,2),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((2,6),(3,1),(4,5),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((1,4),(2,3),(6,5),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((3,6),(4,2),(5,1),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0))),

                        (((1,8),(2,7),(3,6),(4,5),(0,0),(0,0),(0,0),(0,0)),
                        ((8,5),(6,4),(7,3),(1,2),(0,0),(0,0),(0,0),(0,0)),
                        ((2,8),(3,1),(4,7),(5,6),(0,0),(0,0),(0,0),(0,0)),
                        ((8,6),(7,5),(1,4),(2,3),(0,0),(0,0),(0,0),(0,0)),
                        ((3,8),(4,2),(5,1),(6,7),(0,0),(0,0),(0,0),(0,0)),
                        ((8,7),(1,6),(2,5),(3,4),(0,0),(0,0),(0,0),(0,0)),
                        ((4,8),(5,3),(6,2),(7,1),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0))),

                        (((1,10),(2,9),(3,8),(4,7),(5,6),(0,0),(0,0),(0,0)),
                        ((10,6),(7,5),(8,4),(9,3),(1,2),(0,0),(0,0),(0,0)),
                        ((2,10),(3,1),(4,9),(5,8),(6,7),(0,0),(0,0),(0,0)),
                        ((10,7),(8,6),(9,5),(1,4),(2,3),(0,0),(0,0),(0,0)),
                        ((3,10),(4,2),(5,1),(6,9),(7,8),(0,0),(0,0),(0,0)),
                        ((10,8),(9,7),(1,6),(2,5),(3,4),(0,0),(0,0),(0,0)),
                        ((4,10),(5,3),(6,2),(7,1),(8,9),(0,0),(0,0),(0,0)),
                        ((10,9),(1,8),(2,7),(3,6),(4,5),(0,0),(0,0),(0,0)),
                        ((5,10),(6,4),(7,3),(8,2),(9,1),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0))),

                        (((1,12),(2,11),(3,10),(4,9),(5,8),(6,7),(0,0),(0,0)),
                        ((11,1),(10,2),(9,3),(8,4),(7,5),(12,6),(0,0),(0,0)),
                        ((1,10),(2,9),(3,8),(4,7),(5,6),(11,12),(0,0),(0,0)),
                        ((9,1),(8,2),(7,3),(6,4),(12,5),(10,11),(0,0),(0,0)),
                        ((1,8),(2,7),(3,6),(5,4),(11,9),(12,10),(0,0),(0,0)),
                        ((7,1),(6,2),(5,3),(4,12),(8,11),(9,10),(0,0),(0,0)),
                        ((1,6),(2,5),(3,4),(11,7),(10,8),(12,9),(0,0),(0,0)),
                        ((5,1),(4,2),(3,12),(6,11),(7,10),(8,9),(0,0),(0,0)),
                        ((1,4),(2,3),(11,5),(10,6),(9,7),(12,8),(0,0),(0,0)),
                        ((3,1),(2,12),(4,11),(5,10),(6,9),(7,8),(0,0),(0,0)),
                        ((1,2),(11,3),(10,4),(9,5),(8,6),(12,7),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0))),

                        (((1,14),(2,13),(3,12),(4,11),(5,10),(6,9),(7,8),(0,0)),
                        ((14,8),(9,7),(10,6),(11,5),(12,4),(13,3),(1,2),(0,0)),
                        ((2,14),(3,1),(4,13),(5,12),(6,11),(7,10),(8,9),(0,0)),
                        ((14,9),(10,8),(11,7),(12,6),(13,5),(1,4),(2,3),(0,0)),
                        ((3,14),(4,2),(5,1),(6,13),(7,12),(8,11),(9,10),(0,0)),
                        ((14,10),(11,9),(12,8),(13,7),(1,6),(3,4),(2,5),(0,0)),
                        ((4,14),(5,3),(6,2),(7,1),(8,13),(9,12),(10,11),(0,0)),
                        ((14,11),(12,10),(13,9),(1,8),(2,7),(3,6),(4,5),(0,0)),
                        ((5,14),(6,4),(7,3),(8,2),(9,1),(10,13),(11,12),(0,0)),
                        ((14,12),(13,11),(1,10),(2,9),(3,8),(4,7),(5,6),(0,0)),
                        ((6,14),(7,5),(8,4),(9,3),(10,2),(11,1),(12,13),(0,0)),
                        ((14,13),(1,12),(2,11),(3,10),(4,9),(5,8),(6,7),(0,0)),
                        ((7,14),(8,6),(9,5),(10,4),(11,3),(12,2),(13,1),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)),
                        ((0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0))),

                        (((1,16),(2,15),(3,14),(4,13),(5,12),(6,11),(7,10),(8,9)),
                        ((1,2),(15,3),(14,4),(13,5),(12,6),(11,7),(10,8),(16,9)),
                        ((3,1),(16,2),(4,15),(5,14),(6,13),(7,12),(8,11),(9,10)),
                        ((1,4),(2,3),(15,5),(14,6),(13,7),(12,8),(11,9),(10,16)),
                        ((5,1),(4,2),(16,3),(6,15),(7,14),(8,13),(9,12),(10,11)),
                        ((1,6),(5,2),(3,4),(15,7),(14,8),(13,9),(12,10),(16,11)),
                        ((7,1),(2,6),(3,5),(4,16),(8,15),(14,9),(10,13),(11,12)),
                        ((1,8),(7,2),(6,3),(5,4),(14,10),(13,11),(15,9),(12,16)),
                        ((9,1),(2,8),(3,7),(4,6),(16,5),(10,15),(11,14),(12,13)),
                        ((1,10),(9,2),(8,3),(7,4),(6,5),(15,11),(14,12),(13,16)),
                        ((11,1),(2,10),(3,9),(4,8),(5,7),(16,6),(12,15),(13,14)),
                        ((1,12),(11,2),(10,3),(9,4),(8,5),(6,7),(15,13),(14,16)),
                        ((13,1),(2,12),(3,11),(4,10),(5,9),(8,6),(7,16),(15,14)),
                        ((1,14),(13,2),(12,3),(11,4),(10,5),(9,6),(7,8),(16,15)),
                        ((15,1),(2,14),(3,13),(4,12),(11,5),(6,10),(9,7),(8,16))));


 Function HoleSpieltag( Klassengroesse:Integer; Heim,Auswaerts:Integer):Integer;
 Function SystemHinrunde( Klassengroesse:Integer; Heim,Auswaerts:Integer):Boolean;
 Function AnzahlSpiele( Klassengroesse, Spieltag:Integer; Nummern: Mannschaftsmenge):Integer;

 implementation

 Uses Classes, UOptionen, Klass;

 Constructor TPaarung.Create(Sp,H,A:Integer;Hinr:Boolean);
 Begin
  Spieltag := Sp;
  Heim := H;
  Auswaerts := A;
  Hinrunde := Hinr;
  OhneTerminVerlegt := False;
  TerminBerechnet := False;
 End;

 Function HoleSpieltag( Klassengroesse:Integer; Heim,Auswaerts:Integer):Integer;
 Var T, I     : Integer;
     Gefunden : Boolean;
 Begin
  HoleSpieltag := -1;
  Klassengroesse := ( Klassengroesse + 1 ) Div 2;
  If ( Klassengroesse In [2..MaxPaarungen] ) Then
   Begin
    T := 1;
    I := 1;
    Gefunden := False;
    While Not Gefunden Do
     If ( (SystemC[Klassengroesse,T,I,Heimmannschaft] = Heim) And
          (SystemC[Klassengroesse,T,I,Auswaertsmannschaft] = Auswaerts ) ) Or
        ( (SystemC[Klassengroesse,T,I,Heimmannschaft] = Auswaerts ) And
          (SystemC[Klassengroesse,T,I,Auswaertsmannschaft] = Heim ) )
          then
      Gefunden := True
     Else
      Begin
       Inc( I );
       If I > Klassengroesse then
        Begin
         I := 1;
         Inc( T );
         If T > 2* Klassengroesse - 1 then
          Begin
           Gefunden := True;
           T := -1;
          End;
        End;
      End;
    HoleSpieltag := T;
   End;
 End;

 Function SystemHinrunde( Klassengroesse:Integer; Heim,Auswaerts:Integer):Boolean;
 Var T, I     : Integer;
     Gefunden : Boolean;
 Begin
  SystemHinrunde := False;
  Klassengroesse := ( Klassengroesse + 1 ) Div 2;
  If ( Klassengroesse In [2..MaxPaarungen] ) Then
   Begin
    T := 1;
    I := 1;
    Gefunden := False;
    While Not Gefunden Do
     If ( (SystemC[Klassengroesse,T,I,Heimmannschaft] = Heim) And
          (SystemC[Klassengroesse,T,I,Auswaertsmannschaft] = Auswaerts ) )
          then
      Gefunden := True
     Else
      Begin
       Inc( I );
       If I > Klassengroesse then
        Begin
         I := 1;
         Inc( T );
         If T > 2* Klassengroesse - 1 then
          Begin
           Gefunden := True;
           T := -1;
          End;
        End;
      End;
    If T >= 0 then SystemHinrunde := True;
   End;
 End;

 Function AnzahlSpiele( Klassengroesse, Spieltag:Integer; Nummern:Mannschaftsmenge):Integer;
 Var Anzahl, T : Integer;
 Begin
  Anzahl := 0;
  Klassengroesse := ( Klassengroesse + 1 ) Div 2;
  If ( Klassengroesse In [2..MaxPaarungen] ) Then
    For T := 1 to Klassengroesse Do
     If ( SystemC[Klassengroesse,Spieltag,T,Heimmannschaft] In Nummern ) And
        ( SystemC[Klassengroesse,Spieltag,T,Auswaertsmannschaft] In Nummern ) Then
      Inc( Anzahl );
  AnzahlSpiele := Anzahl;
 End;

end.
